package database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import entity.GoodsInfo;

public class ShoppingDBHelper extends SQLiteOpenHelper {
    private static final String TABLE_GOODS_INFO = "goods_info";
    private static final String TABLE_CART_INFO = "cart_info";

    private static final String DB_NAME = "shopping.db";
    private static final int DB_VERSION = 1;
    private static ShoppingDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;

    private ShoppingDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static ShoppingDBHelper getInstance(Context context){
        if (mHelper == null){
            mHelper = new ShoppingDBHelper(context);
        }
        return mHelper;
    }
    public SQLiteDatabase OpenReadLink(){
        if (mRDB == null){
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }
    public SQLiteDatabase OpenWriteLink(){
        if (mWDB == null){
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建商品信息表
        String sql = "create table "+TABLE_GOODS_INFO +
                "( id integer primary key autoincrement not null,"+
                "name varchar not null," +
                "description varchar not null," +
                "price float not null," +
                "pic_path varchar not null);";
        db.execSQL(sql);
        // 创建购物车
        sql = "create table "+TABLE_CART_INFO +
                "( id integer primary key autoincrement not null,"+
                "goods_id integer not null," +
                "count integer not null);" ;
        db.execSQL(sql);
    }
    public void closeLink(){
        if (mWDB != null){
            mWDB.close();
        }
        if (mRDB != null){
            mRDB.close();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    //商品信息表存入数据库
    public void insertGoodsInfo(List<GoodsInfo> list){
        try{
            mWDB.beginTransaction();
            for (GoodsInfo info :list){
                ContentValues values = new ContentValues();
                values.put("name",info.name);
                values.put("price",info.price);
                values.put("pic_Path",info.picPath);
                values.put("pic",info.pic);
                values.put("description",info.description);
                mWDB.insert(TABLE_GOODS_INFO,null,values);
            }
            mWDB.setTransactionSuccessful();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }
    }

    //查询所有商品信息
    public List<GoodsInfo> queryAllGoodsInfo() {
        Log.e("TAG", "queryAllGoodsInfo: 666666666" );
        String sql = "select * from "+TABLE_GOODS_INFO;
        List<GoodsInfo> list = new ArrayList<>();
        Cursor cursor = mRDB.rawQuery(sql, null);
        while (cursor.moveToNext()){
            GoodsInfo info = new GoodsInfo();
            info.id = cursor.getInt(0);
            info.name = cursor.getString(1);
            info.description = cursor.getString(2);
            info.price = cursor.getFloat(3);
            info.picPath = cursor.getString(4);
            Log.e("TAG", "queryAllGoodsInfo: "+info );
            list.add(info);
        }
        cursor.close();
        return list;
    }
}
